<!-- 验证插件 -->
<script src="/static/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="/static/plugins/jquery-validation/localization/messages_zh.min.js"></script>
<!-- 针对当前主题进行简单封装，精简代码 -->
<script src="/static/sdtheme/scripts/sdvalidate.js"></script>

<script>
    var index = parent.layer.getFrameIndex(window.name);

    $(function () {
        pageInit()
    });
    
    function pageInit() {
        CategorySelectInit('{{.m.CategoryId}}');
        $('#IfSchedule').selectpicker('val', {{.m.IfSchedule}});
        validateInfo()
    }

    //
    function CategorySelectInit(selected) {
        var url = '{{urlfor "NotificationCategoryController.DataList"}}';
        var $select = $('#CategoryId');
        $select.html('');

        $.sdpost(url, {}, function (res) {
            if (res.code === 0) {
                var html = [];
                $(res.obj).each(function (i, e) {
                    html.push('<option value="' + e.Id + '">' + e.Name + '</option>')
                });
                $select.html(html.join(''));

                $select.selectpicker({liveSearch: true});
                $select.selectpicker('val', selected)
            } else {
                layer.alert("获取数据失败", {icon: 2, title: '失败'});
            }
        })
    }

    function validateInfo() {
        var $thisForm = $('#thisForm');
        $thisForm.sdvalidate({
            rules: {
                Title: {
                    required: true
                }
            },
            submitHandler: function (form) {
                var url = '{{urlfor "NotificationController.Edit"}}';
                var params = $thisForm.serialize();
                $.sdpost(url, params, function (res) {
                    if (res.code === 0) {
                        parent.layer.msg(res.msg)
                        parent.refresh(res.obj)
                        parent.layer.close(index)
                    } else {
                        parent.layer.alert(res.msg, {icon: 2, title: '失败'})
                    }
                });
                return false
            }
        });
    }
    
    function FormSubmit() {
        $('#thisForm').submit()
    }
</script>